package com.luoying.itew.http;

import com.luoying.itew.util.LogUtils;

import okhttp3.logging.HttpLoggingInterceptor;

public class HttpLogger implements HttpLoggingInterceptor.Logger {
    private static final String TAG = "HttpLogger";
    private StringBuffer messageLog = new StringBuffer();

    @Override
    public void log(String message) {

        if(message.startsWith("--> GET")||message.startsWith("--> POST")){
            messageLog.setLength(0);
            messageLog.append(" ").append("\n");
            messageLog.append("<---- Request start ---->").append("\n");
        }

        if(message.startsWith("<--")&&!message.startsWith("<-- END HTTP")){
            messageLog.setLength(0);
            messageLog.append(" ").append("\n");
            messageLog.append("<---- Response start ---->").append("\n");
        }

        messageLog.append(message).append("\n");

        if(message.startsWith("--> END GET")||message.startsWith("--> END POST")){
            messageLog.append("<---- Request end---->").append("\n");
            LogUtils.d(TAG, messageLog.toString());
        }

        if(message.startsWith("<-- END HTTP")){
            messageLog.append("<---- Response end---->").append("\n");
            LogUtils.d(TAG, messageLog.toString());
        }
    }
}
